package com.hc.acoustics.widget

import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.hc.acoustics.R
import com.hc.acoustics.util.UiHelperUtil
import com.yc.yclibx.comment.YcResources
import kotlinx.android.synthetic.main.weight_text_edit.view.*

/**
 *
 */
class RadioView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var mIsSelect: Boolean = true
        get() {
            return field
        }
    private var radioText: String = ""
    private var textView: TextView
    private var mImageView: ImageView

    init {
        //        addView(YcResources.createView(getContext(), R.layout.weight_text_edit))
        //        @SuppressLint("Recycle")
        val a = context.obtainStyledAttributes(attrs, R.styleable.RadioView, defStyleAttr, 0)
        val text = a.getString(R.styleable.RadioView_radio_text)
//        val textSize = a.getDimension(R.styleable.RadioView_radio_text_size, 18f)
        radioText = when (text) {
            null -> ""
            else -> text
        }
        textView = TextView(getContext())
        textView.text = radioText
        textView.setTextSize(
            TypedValue.COMPLEX_UNIT_PX,
            resources.getDimensionPixelOffset(R.dimen.radio_size).toFloat()
        )
        textView.setTextColor(YcResources.getColor(R.color.textColorViolet))
        textView.setGravity(Gravity.CENTER_VERTICAL)
        addView(textView)

        mImageView = ImageView(getContext())
        mImageView.setPadding(resources.getDimensionPixelOffset(R.dimen.margin_8dp), 0, 0, 0)

        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER)
        mImageView.setAdjustViewBounds(true)
        val imageSize = resources.getDimensionPixelOffset(R.dimen.radio_image_size)
        mImageView.setLayoutParams(ViewGroup.LayoutParams(imageSize, imageSize))
        mIsSelect = a.getBoolean(R.styleable.RadioView_radio_is_select, true)
        setSelect(mIsSelect)
        addView(mImageView)
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
    }

    public fun setSelect(isSelect: Boolean) {
        mIsSelect = isSelect
        if (isSelect) {
            mImageView.setImageResource(R.drawable.ic_select)
        } else {
            mImageView.setImageResource(R.drawable.ic_select_un)
        }
    }

    public fun isSelect(): Boolean {
        return mIsSelect
    }
}